package com.liangzong.betspider.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class ProcessDataUtil {
    public static String getStrByReg(String msg, String reg) {
        // 匹配当前正则表达式
        Matcher matcher = Pattern.compile(reg).matcher(msg);
        // 定义当前文件的文件名称
        String fileName = "";
        // 判断是否可以找到匹配正则表达式的字符
        if (matcher.find()) {
            // 将匹配当前正则表达式的字符串即文件名称进行赋值
            fileName = matcher.group(1);
        }
        // 返回
        return fileName;
    }

    public static JSONObject str2obj(String comp) {
        JSONObject obj = new JSONObject();
        String[] split = comp.split(";");
        for (String s : split) {
            String[] entry = s.split("=");
            String key = entry[0];
            String value = null;
            if (entry.length > 1) {
                value = entry[1];
            }
            obj.put(key, value);
        }
        return obj;
    }

    public static void pause() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static BigDecimal transferOdds(String odds) {
        if (odds == null) {
            return new BigDecimal(0);
        }
        String[] split = odds.split("/");
        return new BigDecimal("1").add(new BigDecimal(split[0]).divide(new BigDecimal(split[1]), 4, RoundingMode.UNNECESSARY));
    }

    /**
     * 计算比赛结束时间 统一用 90分钟计算
     *
     * @param time
     * @return
     */
    public static LocalDateTime calculateOverTime(String time, int overTime) {
        String[] split = time.split(":");
        if(split.length!=2){
            log.error("time 格式错误time={}"+time);
            return null;
        }
        LocalDateTime now = LocalDateTime.now();
        String min = split[0];
        String ss = split[1];
        int remindMin = new BigDecimal((overTime - 1) + "").subtract(new BigDecimal(min)).intValue();
        int remindSec = new BigDecimal("60").subtract(new BigDecimal(ss)).intValue();
        LocalDateTime overTimePoint;
        overTimePoint = now.plusMinutes(Math.max(0, remindMin));
        overTimePoint = overTimePoint.plusSeconds(Math.max(0, remindSec));
        return overTimePoint;
    }
}
